//
//  Base.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/16.
//

#import "Base.h"
#import "LocationUpdata.h"
#import "DataFrameParam.h"
#import <CoreLocation/CoreLocation.h>
@interface Base ()<CLLocationManagerDelegate>
@property (strong, nonatomic)CLLocationManager *localMag;
@property (strong, nonatomic)NSString *markTagMsg;
@end

@implementation Base


-(void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self upMark];
}
-(void)navUI
{
    UIView *view = [UIView new];
    view.backgroundColor = UIColor.clearColor;
    [self.view addSubview:view];
    _navBgView = view;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_offset(0);
        make.height.mas_equalTo(MarginTop + 64);
    }];
    [view addSubview:self.navView];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_equalTo(MarginTop + 20);
        make.height.mas_equalTo(44);
    }];

    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:back];
    [back setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.centerY.mas_equalTo(self.navView);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    _back_btn = back;
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.navTitle];
    [self.navTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.navView);
    }];
    self.navTitle.text = self.navtitleText;
}
-(void)setNamess:(NSString *)namess
{
    self.navTitle.text = namess;

}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(UIView *)navView
{
    if (!_navView) {
        _navView = [UIView new];
        _navView.backgroundColor = UIColor.clearColor;
    }
    return _navView;
}
-(UILabel *)navTitle
{
    if (!_navTitle) {
        _navTitle = [UILabel new];
        _navTitle.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
        _navTitle.textColor = HexColor(@"#252628");
    }
    return _navTitle;
}

- (void)showAutoMessage:(NSString *)message ToView:(UIView *)view{
    [self showMessage:message ToView:view RemainTime:1.5 Model:MBProgressHUDModeText];
}


-(void)showMessage:(NSString *)message ToView:(UIView *)view RemainTime:(CGFloat)time Model:(MBProgressHUDMode)model{
    if (view == nil) view = (UIView*)[UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.tintColor = [UIColor whiteColor];
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = HexColor(@"#252628");
    hud.bezelView.alpha = 0.8;
    hud.label.text = message;
    hud.label.font = [UIFont systemFontOfSize:14];
    hud.label.numberOfLines = 0;
    hud.mode = model;
    hud.removeFromSuperViewOnHide = YES;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:time];
}


- (void)startMark:(NSString *)markType {
    self.markTagMsg = markType;
    NSString *lat = [LocationUpdata sharedInstance].lat;
    NSString *lot = [LocationUpdata sharedInstance].lot;
    if(lat == nil || [lat isEqualToString:@""]){
        [self startUpMarkLat:@"0" lot:@"0"];
    } else {
        [self startUpMarkLat:lat lot:lot];
    }
}

#pragma mark -For Home-
- (void)upMark {
    if(!self.localMag){
        self.localMag = [[CLLocationManager alloc] init];
        [self.localMag requestWhenInUseAuthorization];
        self.localMag.desiredAccuracy = kCLLocationAccuracyBest;
        self.localMag.delegate = self;
    }
    if (@available(iOS 14.0, *)) {
        if(self.localMag.authorizationStatus == kCLAuthorizationStatusDenied){
//            [self startUpMarkLat:@"0" lot:@"0"];
        } else {
            [self.localMag startUpdatingLocation];
        }
    } else {
        // Fallback on earlier versions
        if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
//            [self startUpMarkLat:@"0" lot:@"0"];
        } else {
            [self.localMag startUpdatingLocation];
        }
        
    }
}
    


- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
    [self.localMag stopUpdatingLocation];
    
    CLLocation *currentLocal = [locations lastObject];
    NSString * lat = [NSString stringWithFormat:@"%f",currentLocal.coordinate.latitude];
    NSString * lot = [NSString stringWithFormat:@"%f",currentLocal.coordinate.longitude];
    [LocationUpdata sharedInstance].lat = lat;
    [LocationUpdata sharedInstance].lot = lot;
    
//    [self startUpMarkLat:lat lot:lot];
    
    
}


- (void)startUpMarkLat:(NSString *)lat lot:(NSString*)lot {
    NSString * end = [NSString stringWithFormat:@"%d",(int)[[NSDate date] timeIntervalSince1970]];
    NSString * start = [LocationUpdata sharedInstance].startMark;
    if(start == nil){
        start = @"";
    }
    NSString * ordId = @"";
    if([self.markTagMsg isEqualToString:@"9"] || [self.markTagMsg isEqualToString:@"10"]){
        start = end;
    }
    if([self.markTagMsg isEqualToString:@"10"] || [self.markTagMsg isEqualToString:@"9"]){
        NSString *ssOrd = [[NSUserDefaults standardUserDefaults] objectForKey:@"orderG"];
        if(ssOrd != nil && ![ssOrd isEqualToString:@""]){
            ordId = ssOrd;
        }
    }
    NSDictionary *param = @{ @"forced":self.markTagMsg, @"todisappoint":@"2",@"inspect":[DataFrameParam armyoure],@"figures":[DataFrameParam even],@"waved" :lot, @"house" :lat, @"poirotthe" : start, @"satellite" : end, @"plate" : ordId};
    NSLog(@"%@",param);
    [SCRequest postHUDWithUrl:@"/cham/ay" params:param isMsg:YES success:^(id  _Nonnull responseObject) {
    } failure:^(NSError * _Nonnull error) {
    }];

}

- (void)MoveSpokes {

    NSString *even =[DataFrameParam even];
    NSString *armyoure = [DataFrameParam armyoure];
    NSLog(@"%@",@{@"ringing":armyoure,@"overbearing":even});
    [SCRequest postHUDWithUrl:@"/cham/spokes" params:@{@"ringing":armyoure,@"overbearing":even} isMsg:YES success:^(id  _Nonnull responseObject) {
    } failure:^(NSError * _Nonnull error) {
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-  (void)startMarkFor8 {
    NSString * end = [NSString stringWithFormat:@"%d",(int)[[NSDate date] timeIntervalSince1970]];
    NSString * start = [LocationUpdata sharedInstance].startMark;
    if(start == nil){
        start = @"";
    }
    NSString * ordId = @"";
    
    NSString * lat = [LocationUpdata sharedInstance].lat;
    NSString * lot = [LocationUpdata sharedInstance].lot;
    NSDictionary *param = @{ @"forced":@"8", @"todisappoint":@"2",@"inspect":[DataFrameParam armyoure],@"figures":[DataFrameParam even],@"waved" :lot, @"house" :lat, @"poirotthe" : start, @"satellite" : end, @"estate" : ordId};
    NSLog(@"%@",param);
    [DataFrameMove frameMoverForPost:@"/cham/ay" MoverPost:YES frameParm:param finish:^(id  _Nonnull info) {
        
    }];
}
@end
